/*
 * Copyright (C) 2009 Rafael Fernandes, Jeff Sharkey, http://jsharkey.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.widgets.desktop;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.RemoteViews;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.activities.main.MainContainerActivity;
import com.phonebooksharing.android.provider.PhonebookSharing.Dashboard;
import com.phonebooksharing.android.services.DesktopDashboardUpdaterService;

/**
 * 
 * @author Jeff Sharkey, http://jsharkey.org/ (Examples)
 * @author Rafael Fernandes (Adapted based upon Sharkey's examples)
 *
 */
public class DashboardAppWidgetProvider extends AppWidgetProvider {

    /**
     * {@inheritDoc}
     */
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // If no specific widgets requested, collect list of all
        if (appWidgetIds == null) {
            appWidgetIds = appWidgetManager.getAppWidgetIds(
                    new ComponentName(context, TextMessageAppWidgetProvider.class));
        }

        // Request update for these widgets and launch updater service
        DesktopDashboardUpdaterService.requestUpdate(appWidgetIds);
        context.startService(new Intent(context, DesktopDashboardUpdaterService.class));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {

    }

    /**
     * Build an update for the given tiny widget. Should only be called from a
     * service or thread to prevent ANR during database queries.
     */
    public static RemoteViews buildUpdate(Context context, Dashboard d) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.dash_desktop_layout);

        if (d != null) {
            views.setViewVisibility(R.id.qty, View.VISIBLE);
            views.setTextViewText(R.id.qty, "" + 
                    (d.qtyFriendsNearby + 
                     d.qtyJournal +
                     d.qtyMessages +
                     d.qtyPhotos +
                     d.qtyPlacesNearby +
                     d.qtyPostcards +
                     d.qtyVideos));
            views.setViewVisibility(R.id.dashboard, View.VISIBLE);
            views.setViewVisibility(R.id.divider, View.VISIBLE);
            
            
            setRemoteValue(views, R.id.friend, R.id.fri_qty, d.qtyFriendsNearby);
            // setRemoteValue(views, R.id.msg, R.id.fri_qty, d.getQtyJournal());
            setRemoteValue(views, R.id.msg, R.id.msg_qty, d.qtyMessages);
            setRemoteValue(views, R.id.photo, R.id.photo_qty, d.qtyPhotos);
            // setRemoteValue(views, R.id.friend, R.id.fri_qty,
            // d.getQtyPlacesNearby());
            setRemoteValue(views, R.id.postcard, R.id.postcard_qty, d.qtyPostcards);
            setRemoteValue(views, R.id.video, R.id.video_qty, d.qtyVideos);

            views.setViewVisibility(android.R.id.empty, View.GONE);
        } else {
            views.setViewVisibility(android.R.id.empty, View.VISIBLE);
        }

        // Connect click intent to launch details
        Intent detailIntent = new Intent(context, MainContainerActivity.class);

        PendingIntent pending = PendingIntent.getActivity(context, 0, detailIntent, 0);
        views.setOnClickPendingIntent(R.id.appwidget, pending);

        return views;
    }

    protected static void setRemoteValue(RemoteViews views, int captionView, int valueView, int qty) {
        if (qty > 0) {
            views.setViewVisibility(valueView, View.VISIBLE);
            views.setViewVisibility(captionView, View.VISIBLE);
            views.setTextViewText(valueView, "" + qty);
        } else {
            views.setViewVisibility(valueView, View.GONE);
            views.setViewVisibility(captionView, View.GONE);
        }
    }
}
